package org.vector.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.vector.dao.BookDao;
import org.vector.utils.BaseDao;

public class BookDaoImpl implements BookDao {
	BaseDao bd = new BaseDao();
	
	@Override
	public int LookId(String id) {
		String sql = "select readerid from reader where readerid = ?";
		ResultSet r = bd.executeQuery(sql, id);
		try {
			if (r.next()) {
				return 1;
			} else {
				return 0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 1;
	}

	/**
	 * 得到所有信息
	 */
	@Override
	public String[][] getRecord() {
		int num = 0;
		// String sql = "select
		// gouwuid,bankid,bankid2,themoney,xiaofei,sserivice,datatime from daytodaycard
		// where bankid = ?";
		String sql = "select * from reader";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][8];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		try {
			while (r.next()) {
				String q = r.getString("readerid");
				String w = r.getString("readertypeid");
				String e = r.getString("readerage");
				String s = r.getString("readeradress");
				String t = r.getString("readername");
				String y = r.getString("readersex");
				String a = r.getString("readerphone");
				String d = r.getString("readerstate");

//				String gouwuidc = Integer.toString(gouwuid);
//				String bankiddc = Integer.toString(bankidd);
//				String bankid2c = Integer.toString(bankid2);
//				String themoneyc = Integer.toString(themoney);
//				String xiaofeic = Integer.toString(xiaofei);

				data[row][0] = q;
				data[row][1] = w;
				data[row][2] = e;
				data[row][3] = s;
				data[row][4] = t;
				data[row][5] = y;
				data[row][6] = a;
				data[row][7] = d;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getRecordone(String lei) {
		int num = 0;
		// String sql = "select
		// gouwuid,bankid,bankid2,themoney,xiaofei,sserivice,datatime from daytodaycard
		// where bankid = ?";
		String sql = "select readerid,readertypeid,readerage,readeradress,readername,readersex,readerphone,readerstate from reader where readertypeid = ?";
		ResultSet resultset = bd.executeQuery(sql, lei);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][8];
		ResultSet r = bd.executeQuery(sql, lei);
		int row = 0;
		try {
			while (r.next()) {
				String q = r.getString("readerid");
				String w = r.getString("readertypeid");
				String e = r.getString("readerage");
				String s = r.getString("readeradress");
				String t = r.getString("readername");
				String y = r.getString("readersex");
				String a = r.getString("readerphone");
				String d = r.getString("readerstate");

//				String gouwuidc = Integer.toString(gouwuid);
//				String bankiddc = Integer.toString(bankidd);
//				String bankid2c = Integer.toString(bankid2);
//				String themoneyc = Integer.toString(themoney);
//				String xiaofeic = Integer.toString(xiaofei);

				data[row][0] = q;
				data[row][1] = w;
				data[row][2] = e;
				data[row][3] = s;
				data[row][4] = t;
				data[row][5] = y;
				data[row][6] = a;
				data[row][7] = d;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getBook() {
		int num = 0;
		// String sql = "select
		// gouwuid,bankid,bankid2,themoney,xiaofei,sserivice,datatime from daytodaycard
		// where bankid = ?";
		String sql = "select * from book";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][8];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		try {
			while (r.next()) {
				String q = r.getString("bookISBN");
				String w = r.getString("booktype");
				String e = r.getString("bookname");
				String s = r.getString("bookauthor");
				String t = r.getString("bookpress");
				String y = r.getString("bookdate");
				String a = r.getString("bookprintnumber");
				String d = r.getString("bookprice");

//				String gouwuidc = Integer.toString(gouwuid);
//				String bankiddc = Integer.toString(bankidd);
//				String bankid2c = Integer.toString(bankid2);
//				String themoneyc = Integer.toString(themoney);
//				String xiaofeic = Integer.toString(xiaofei);

				data[row][0] = q;
				data[row][1] = w;
				data[row][2] = e;
				data[row][3] = s;
				data[row][4] = t;
				data[row][5] = y;
				data[row][6] = a;
				data[row][7] = d;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getUser() {
		int num = 0;
		// String sql = "select
		// gouwuid,bankid,bankid2,themoney,xiaofei,sserivice,datatime from daytodaycard
		// where bankid = ?";
		String sql = "select * from user";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][3];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		int cc = 1;
		try {
			while (r.next()) {

				String w = r.getString("name");
				String e = r.getString("password");

//				String gouwuidc = Integer.toString(gouwuid);
//				String bankiddc = Integer.toString(bankidd);
//				String bankid2c = Integer.toString(bankid2);
//				String themoneyc = Integer.toString(themoney);
//				String xiaofeic = Integer.toString(xiaofei);
				String ccc = Integer.toString(cc);
				data[row][0] = ccc;
				data[row][1] = w;
				data[row][2] = e;

				row++;
				cc++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getBookType() {
		int num = 0;
		String sql = "select * from booktype";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][2];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		try {
			while (r.next()) {

				String w = r.getString("booktype");
				String e = r.getString("bookname");

				data[row][0] = w;
				data[row][1] = e;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getReaderType() {
		int num = 0;
		String sql = "select * from readertype";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][4];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		try {
			while (r.next()) {

				String a1 = r.getString("readerid");
				String a2 = r.getString("readertype");
				String a3 = r.getString("maxbook");
				String a4 = r.getString("loneday");
				data[row][0] = a1;
				data[row][1] = a2;
				data[row][2] = a3;
				data[row][3] = a4;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}
	@Override
	public String[][] getReaderType(String readertype) {
		int num = 0;
		String sql = "select readerid,readertype,maxbook,loneday from readertype where readertype = ?";
		ResultSet resultset = bd.executeQuery(sql,readertype);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][4];
		ResultSet r = bd.executeQuery(sql,readertype);
		int row = 0;
		try {
			while (r.next()) {

				String a1 = r.getString("readerid");
				String a2 = r.getString("readertype");
				String a3 = r.getString("maxbook");
				String a4 = r.getString("loneday");
				data[row][0] = a1;
				data[row][1] = a2;
				data[row][2] = a3;
				data[row][3] = a4;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}
	@Override
	public String[][] getBookBorror() {
		int num = 0;
		String sql = "select * from book";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][3];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		try {
			while (r.next()) {
				Date da = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmm");
				String cd = sdf.format(da);

				String a1 = r.getString("bookISBN");
				String a2 = r.getString("bookname");
				// String a3 = r.getString("borrortime");
				data[row][0] = a1;
				data[row][1] = a2;
				data[row][2] = cd;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getBook(String bookISBN) {
		int num = 0;
		String sql = "select bookISBN,booktype,bookname,bookauthor,bookpress,bookdate,bookprintnumber,bookprice from book where bookISBN = ?";
		ResultSet resultset = bd.executeQuery(sql, bookISBN);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][8];
		ResultSet r = bd.executeQuery(sql, bookISBN);
		int row = 0;
		try {
			while (r.next()) {
				String q = r.getString("bookISBN");
				String w = r.getString("booktype");
				String e = r.getString("bookname");
				String s = r.getString("bookauthor");
				String t = r.getString("bookpress");
				String y = r.getString("bookdate");
				String a = r.getString("bookprintnumber");
				String d = r.getString("bookprice");

				data[row][0] = q;
				data[row][1] = w;
				data[row][2] = e;
				data[row][3] = s;
				data[row][4] = t;
				data[row][5] = y;
				data[row][6] = a;
				data[row][7] = d;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getBookCome() {
		int num = 0;
		String sql = "select * from bookborror";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][3];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		try {
			while (r.next()) {
				String a1 = r.getString("bookISBN");
				String a2 = r.getString("bookname");
				String a3 = r.getString("borrortime");
				// String a4 = r.getString("readerid");
				// String a5 = r.getString("readername");
				// String a6 = r.getString("readertype");

				data[row][0] = a1;
				data[row][1] = a2;
				data[row][2] = a3;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getBookCome2() {
		int num = 0;
		String sql = "select * from bookborror";
		ResultSet resultset = bd.executeQuery(sql);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][6];
		ResultSet r = bd.executeQuery(sql);
		int row = 0;
		try {
			while (r.next()) {
				String a1 = r.getString("bookISBN");
				String a2 = r.getString("bookname");
				String a3 = r.getString("borrortime");
				String a4 = r.getString("readerid");
				String a5 = r.getString("readername");
				String a6 = r.getString("readertype");

				data[row][0] = a1;
				data[row][1] = a2;
				data[row][2] = a3;
				data[row][3] = a4;
				data[row][4] = a5;
				data[row][5] = a6;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getBookone(String booktype) {
		int num = 0;
		String sql = "select bookISBN,booktype,bookname,bookauthor,bookpress,bookdate,bookprintnumber,bookprice from book where booktype = ?";
		ResultSet resultset = bd.executeQuery(sql, booktype);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][8];
		ResultSet r = bd.executeQuery(sql, booktype);
		int row = 0;
		try {
			while (r.next()) {
				String q = r.getString("bookISBN");
				String w = r.getString("booktype");
				String e = r.getString("bookname");
				String s = r.getString("bookauthor");
				String t = r.getString("bookpress");
				String y = r.getString("bookdate");
				String a = r.getString("bookprintnumber");
				String d = r.getString("bookprice");

				data[row][0] = q;
				data[row][1] = w;
				data[row][2] = e;
				data[row][3] = s;
				data[row][4] = t;
				data[row][5] = y;
				data[row][6] = a;
				data[row][7] = d;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	@Override
	public String[][] getBookTypeOne(String bookname) {
		int num = 0;
		String sql = "select booktype,bookname from booktype where booktype = ?";
		ResultSet resultset = bd.executeQuery(sql,bookname);
		try {
			while (resultset.next()) {
				num++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String[][] data = new String[num][2];
		ResultSet r = bd.executeQuery(sql,bookname);
		int row = 0;
		try {
			while (r.next()) {

				String w = r.getString("booktype");
				String e = r.getString("bookname");

				data[row][0] = w;
				data[row][1] = e;

				row++;
			}
			return data;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return data;
	}

	

}
